package com.treasure.service.loan;

import com.treasure.service.annotation.LogInfo;
import com.treasure.service.dto.bank91.CreditApplyFor91Dto;
import com.treasure.service.third.utils.RestClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/** 
* @ClassName    : LoanController 
* @author       : panxingwu 
* @date         : 2018年12月9日 下午1:44:21 
* @Description  : 贷款业务
*/
@Api(value = "贷款业务",description = "贷款业务")
@RestController
@RequestMapping("/loan/v1")
@Slf4j
public class LoanController {

    @Autowired
    private RestClient restClient;

    @Autowired
    RestTemplate template;

    @ApiOperation(value = "获取贷款列表", notes = "获取贷款列表")
    @RequestMapping("/applyFor")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "頁碼", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每頁顯示數量", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "当前状态(0-禁用 1-启用)", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "recommend", value = "是否首页展示(0-否 1-是)", required = true, dataType = "int", paramType = "query"),
    })
    @LogInfo(log = "获取贷款列表", isPrint = true)
    public void applyFor(HttpServletResponse response) throws IOException {

        CreditApplyFor91Dto applyFor91Dto = new CreditApplyFor91Dto();
//        applyFor91Dto.setName("123");
//        applyFor91Dto.setIdcard("420621198605121278");
//        applyFor91Dto.setTel("19919866605");
//        applyFor91Dto.setBankid("1");
//        applyFor91Dto.setToken("4fff6d7383574d08b0470f37e5baa72c");

        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
        paramMap.add("token","4fff6d7383574d08b0470f37e5baa72c");
        paramMap.add("bankid","1");
        paramMap.add("name","潘新文");
        paramMap.add("idcard","420621198605121278");
        paramMap.add("tel","19919866605");
        String url ="https://91k.91kab.com/oauth/apply2";
//        String result = restClient.get(url, new HashMap<>(), String.class);

        response.sendRedirect(url+"");


        RestTemplate template = new RestTemplate();
        // 封装参数，千万不要替换为Map与HashMap，否则参数无法传递
//        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
        paramMap.add("dt", "20180416");

        // 1、使用postForObject请求接口
        String result = template.postForObject(url, paramMap, String.class);
        System.out.println("result1==================" + result);

        // 2、使用postForEntity请求接口
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap,headers);
        ResponseEntity<String> response2 = template.postForEntity(url, httpEntity, String.class);
        System.out.println("result2====================" + response2.getBody());

        // 3、使用exchange请求接口
        ResponseEntity<String> response3 = template.exchange(url, HttpMethod.POST, httpEntity, String.class);
        System.out.println("result3====================" + response3.getBody());

    }
}
